﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-03-23
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System.IO;
using UnityEngine;
using System.Text;
using System;
namespace IFramework
{
	public class FormatUserScriptProcessor : UnityEditor.AssetModificationProcessor
    {
        public static void OnWillCreateAsset(string newFileMeta)
        {
            string newFilePath = newFileMeta.Replace(".meta", "");
            string fileExt = Path.GetExtension(newFilePath);
            if (fileExt != ".cs") return;
            //注意，Application.datapath会根据使用平台不同而不同
            string realPath = Application.dataPath.Replace("Assets", "") + newFilePath;
            string txt = File.ReadAllText(realPath);
            //这里实现自定义的一些规则
            txt = txt.Replace("#User#", ProjectInfo.projectSetting.UserName);

            txt = txt.Replace("#UserSCRIPTNAME#", Path.GetFileNameWithoutExtension(newFilePath));
            txt = txt.Replace("#UserNameSpace#", ProjectInfo.projectSetting.NameSpace);
            txt = txt.Replace("#UserVERSION#", ProjectInfo.projectSetting.Version);
            txt = txt.Replace("#UserUNITYVERSION#", Application.unityVersion);
            txt = txt.Replace("#UserDATE#", DateTime.Now.ToString("yyyy-MM-dd"));

            File.WriteAllText(realPath, txt, Encoding.UTF8);
        }
    }
}
